//
//  XOCartViewController.m
//  didigo
//
//  Created by JS_Coder on 2019/2/19.
//  Copyright © 2019 lifayu. All rights reserved.
//

#import "XOCartViewController.h"
#import "XOCartShopTableCell.h"
#import "XOShoppingCartListRequest.h"
#import "XOShoppingCartModel.h"
#import "XOShoppingCartViewController.h"
#import "XOShopCartViewModel.h"
#import "EJSShoppingCarSingleton.h"


@interface XOCartViewController ()
{
    UIView *_noDataView;
}

@property (strong, nonatomic) XOShoppingCartListModel *shoppingCartListModel;
@end

@implementation XOCartViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noti_updateShoppingCarList) name:Noti_UpdateShoppingCarList object:nil];
    [self configure];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self loadShoppingCartList];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)configure {
    self.tableView.backgroundColor = UIColorFromRGB(0xF0F0F0);
    self.title = @"购物车";
    [self.tableView registerNib:[UINib nibWithNibName:@"XOCartShopTableCell" bundle:nil] forCellReuseIdentifier:@"XOCartShopTableCell"];
    self.tableView.rowHeight = 130;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

#pragma mark - loadData
- (void)loadShoppingCartList{
    if(![EJSUserTokenModel userTokenModel]){
        return;
    }

    FlyWeakSelf
    [[EJSShoppingCarSingleton sharedSingleton]updateShoppingList:^(XOShoppingCartListModel * modelList) {
        [weakSelf noti_updateShoppingCarList];
    } failHandler:^(BaseResponse *response) {
        [[HUDHelper sharedInstance] tipMessage:response.message];
    }];
}
    
-(void)noti_updateShoppingCarList{
    self.shoppingCartListModel = [EJSShoppingCarSingleton sharedSingleton].shoppingList;
    [self.navigationController.tabBarItem setBadgeColor:AppSecondMainColor];
    if(self.shoppingCartListModel.list.count > 0){
        [self hideNoDataView];
        [self.navigationController.tabBarItem setBadgeValue:
         [NSString stringWithFormat:@"%lu", (unsigned long)self.shoppingCartListModel.total.intValue]];
    }else{
        [self.navigationController.tabBarItem setBadgeValue:NULL];
        [self showNoDataViewWithText:@"暂无购物车商品"];
    }
    [self updateUI];
}

- (void) updateUI {
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.shoppingCartListModel.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    XOCartShopTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XOCartShopTableCell" forIndexPath:indexPath];
    XOShoppingCartModel* shopModel = self.shoppingCartListModel.list[indexPath.row];
    cell.shopModel = shopModel;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    XOShoppingCartModel* shopModel = self.shoppingCartListModel.list[indexPath.row];
    XOShoppingCartViewController *shopCartVC = [UIStoryboard storyboardWithName:@"Main" bundle:nil].instantiateInitialViewController;
    shopCartVC.shopId = shopModel.shopId;
    [self.navigationController pushViewController:shopCartVC animated:YES];
}

- (void)showNoDataViewWithText:(NSString *)text
{
    [self showNoDataViewWithText:text offset:0];
}

- (void)showNoDataViewWithText:(NSString *)text offset:(CGFloat)offset
{
    [self hideNoDataView];

    _noDataView = [[UIView alloc] initWithFrame:self.view.bounds];
    _noDataView.userInteractionEnabled = NO;
    _noDataView.backgroundColor = kClearColor;
    [self.view addSubview:_noDataView];

    NSString *noDataMsg = text?text:@"暂无数据";

    UILabel *textLabel = [[UILabel alloc] init];
    textLabel.textColor = UIColorFromRGB(0xA4A4A4);
    textLabel.font = kSystemFontSize(15);
    textLabel.text = noDataMsg;
    [_noDataView addSubview:textLabel];

    [textLabel sizeToFit];
    textLabel.center = _noDataView.center;
    textLabel.top += offset/2;
    if (textLabel.bottom + 20 > self.view.height)
      {
        textLabel.bottom = self.view.height - 20;
      }

    if (textLabel.top - 20 < 0)
      {
        textLabel.top = 20;
      }

    UIImageView *imageView = [[UIImageView alloc] init];
    imageView.image = [UIImage imageNamed:@"无数据"];
    [imageView sizeToFit];
    imageView.center = _noDataView.center;
    imageView.bottom = textLabel.top - 10;
    [_noDataView addSubview:imageView];
}

- (void)hideNoDataView
{
    if(_noDataView)
      {
        [_noDataView removeFromSuperview];
        _noDataView = nil;
      }
}
@end
